<!DOCTYPE html>
<html>
<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="/js/jquery/jquery.min.js"></script>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
    <link rel="stylesheet" href="/css/reset.css" />
    <link rel="stylesheet" href="/css/userTravels.css" />
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css" />
    <link rel="stylesheet" href="/js/plugins/eleditor/layout/base.css" />
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/plugins/My97DatePicker/calendar.js"></script>
    <script src="/js/common.js"></script>
    <script>
        var state; // 提交状态 0 草稿 1 待发布

        $(function () {
            // 因为登陆的时候，已经在sessionStorage中存储了user, 所以可以直接获取域中存储的user
            // SessionStorage.getItem 拿到的是字符串 ， JSON.parse 将字符串转化成JS对象
            var user = JSON.parse(sessionStorage.getItem("user"));

            // 设置表单的action
            $("#detailForm").attr("action", "/users/" + user.id + "/travels");

            // 初始化富文本编辑器
            var edr = new Eleditor({
               el:"#contentEditor",
               upload:{
                  server:"/images", // 上传访问的资源
                  formName:"pic"   // 上传参数名称
                }
            });

            // 加载所有旅游区域，回填到旅游地点上
            $.get("/regions", function (data) {
                $.each(data, function (index, ele) {
                    var op = $("<option></option>").html(ele.name).val(ele.id);
                    op.appendTo("#detailForm select[name='place.id']");
                });
            });


            // 图片控件值改变事件
            $("#imgForm input[name='pic']").change(function () {
                if(this.value){
                    $("#imgForm").ajaxSubmit(function (data) {
                        $("#coverUrl").attr("src", data.url);
                    });
                }
            });

            // 点击更新封面按钮，弹出文件上传选择器
            $("#changeBtn").click(function () {
                $("#imgForm input[name='pic']").click();
            });

            // 保存按钮：state = 0
            $("#save").click(function () {
                state = 0;
                $("#detailForm").submit();
            });


            // 发布按钮：state = 0
            $("#release").click(function () {
                state = 1;
                $("#detailForm").submit();
            });


            // 拿到id,ajax请求获得游记数据，来做回显，要放在地址加载的后面，才能拿到地址
            // 如果是编辑操作，需要拿到游记的id,该怎么拿到呢？通过截取url
            var id = getParams().id;
            // 编辑操作
            if (id) {
                // 提交ajax请求，获取到游记的内容,用来回显
                $.get("/travels/"+id, function (data) {
                    // console.log(data);
                    // 回显游记内容
                    $(".travels").renderValues(data);

                },"json");
                // 更新操作，肯定要回显id，以携带
                $("#detailForm input[name='id']").val(id);
            }

            // 改为异步表单
            $("#detailForm").ajaxForm({
                // 表单提交之前调用的函数，目前只用到提交额外参数的功能
                beforeSubmit:function (arr) {
                    // 往数组中添加参数
                    arr.push({name:"title", value:$("#title").val()});
                    arr.push({name:"coverUrl", value:$("#coverUrl").attr("src")});
                    arr.push({name:"travelContent.content", value:edr.getContent()});
                    arr.push({name:"state", value:state});
                    // 有id,就是更新操作，应该把请求改为PUT
                    if(id){
                        arr.push({name:"_method", value:"PUT"});

                    }
                },
                // 请求正常调用的函数
                success:function (data) {
                    $(document).dialog({
                        type:"confirm",
                        style:"IOS",
                        titleShow:false,
                        content:"提交成功",
                        buttons:[{
                            name:"跳转到我的游记",
                            callback:function () {
                                // 2.回到个人信息界面
                                location.href = "/mine/travels.html";
                            }
                        }]
                    });
                }
            });
        });
    </script>
    <script src="../js/plugins/My97DatePicker/calendar.js"></script>

</head>

<body>
	<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
		<input type="file" name="pic" style="display: none;" accept="image/*"/>
	</form>

    <div class="search-head">
        <div class="row nav-search">
            <div class="col-2">
                <a href="javascript:window.history.go(-1);">
                    <span><i class="fa fa-times"></i></span>
                </a>
            </div>
            <div class="col-5"></div>
            <div class="col" style="text-align: right;">
                <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
                <i class="fa fa-check" id="release"><span>发布</span></i>
            </div>
        </div>
    </div>

    <div class="travels form-group">
        <div class="cover">
            <img id="coverUrl" render-src="coverUrl" width="100%" height="200px" />
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </div>

        <div class="title">
            <input class="form-control" id="title" render-value="title" placeholder="请输入标题"/>
        </div>

        <h6 href="javascript:">点击文章内容即可编辑</h6>

		<!--富文本编辑器-->
        <div id="contentEditor" render-html="travelContent.content"></div>

        <hr/>
        <div class="container">
            <form id="detailForm" method="post" >
                <input type="hidden" name="id" />
                <label>出行时间:</label>
                <input type="text" name="travelTime"  class="form-control" render-value="travelTime"
                    onclick="WdatePicker({readOnly:true, maxDate:new Date()});">
                <label>出行天数:</label>
                <input type="number" name="days" class="form-control" render-value="days">
                <label>参与人物:</label>
                <select class="c-select form-control" name="person" render-value="person">
                    <option value="1">一个人的旅行</option>
                    <option value="2">和父母</option>
                    <option value="3">和朋友</option>
                    <option value="4">和同事</option>
                    <option value="5">和爱人</option>
                    <option value="6">和驴友</option>
                </select>
                <label>人均费用:</label>
                <input type="number" name="perExpends" class="form-control" render-value="perExpends">

                <label>旅游地点:</label>
                <select class="form-control" name="place.id" render-value="place.id">
                </select>

                <div class="form-check">
                    <label class="form-check-label">
                        <input class="form-check-input" type="checkbox" name="isPublic" render-attr="checked=isPublic">是否公开
                    </label>
                </div>
            </form>
        </div>
        <hr/>
    </div>
</body>
</html>